package com.snopy.mq.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Consumer
 * @description
 * @email 77912204@qq.com
 * @date 2022/05/29 15:45
 */
public class Recv {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("47.101.45.196");
        factory.setPort(5672);
        try(Connection connection = factory.newConnection();Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            DeliverCallback deliverCallback = (consumerTag,delivery)->{
                String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
                System.out.println("[x] Received :"+msg);
            };
            channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag->{});
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}
